from typing import List
from pydantic import BaseModel, Field
from pydantic_settings import BaseSettings
import yaml


class UserAccountConfig(BaseModel):
    """QQ 用户账户配置"""
    appName: str = Field(..., alias="appName")
    QNumber: str = Field(..., alias="QNumber")
    QPassword: str = Field(..., alias="QPassword")
    weatherGroup: List[str] = Field(..., alias="weatherGroup")


class AppConfig(BaseModel):
    """应用基础配置"""
    name: str = Field("qq", alias="name")
    timeout: int = Field(30, alias="timeout")


class ApplicationConfig(BaseSettings):
    """全局应用配置"""
    userAccount: UserAccountConfig
    app: AppConfig

    @classmethod
    def load_config(cls, config_path: str = "../_qAutomation/application.yml") -> "ApplicationConfig":
        # 读取 YAML 文件
        with open(config_path, "r", encoding='utf-8') as f:
            config_data = yaml.safe_load(f)

        # 自动转换下划线命名到驼峰命名
        return cls(**config_data)


# 初始化配置
config = ApplicationConfig.load_config()

# 访问配置示例
# print(f"应用名称: {config.app.name}")
# print(f"QQ 号码: {config.userAccount.QNumber}")
# print(f"天气推送群组: {config.userAccount.weatherGroup}")

appName = config.app.name
qqNumber = config.userAccount.QNumber
qqPassword = config.userAccount.QPassword
weatherGroup = config.userAccount.weatherGroup